
package gui;


import gui.Jd_add_empleado;
import gui.Jd_modificar_empleado;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import modelo.TableModelEmpleados;
import persistencia.EmpleadosDAO;
import persistencia.TipoEmpleadoDAO;
import pojos.Empleados;
import pojos.Tipoempleado;


/**
 *
 * @author alejandrolopezdelolmo
 */
public class JpConsultarEmpleado extends javax.swing.JPanel {

    
    /**
     * Creates new form PanelSearchBook
     */
    JPanel panelPadre;
    ArrayList<Empleados> alEmpleados;
    
    public JpConsultarEmpleado() {
        initComponents();
        alEmpleados=new ArrayList();
        //agrupo los radio button
        ButtonGroup bg=new ButtonGroup();
        bg.add(this.jcbCodEmple);
        bg.add(this.jcbDni);
        bg.add(this.jcbTipoemple);
        bg.add(this.jcbApellido1);
        
        //dehabilito resto de textField
        jcbCodEmple.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfCodEmple.setEnabled(true);
                jtfApellido1.setText("");
                jtfApellido1.setEnabled(false);
                jtfDni.setText("");
                jtfDni.setEnabled(false);
            }
        });
        jcbDni.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfDni.setEnabled(true);
                jtfCodEmple.setText("");
                jtfCodEmple.setEnabled(false);
                jtfApellido1.setText("");
                jtfApellido1.setEnabled(false);
            }
        });
        jcbTipoemple.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfCodEmple.setText("");
                jtfCodEmple.setEnabled(false);
                jtfDni.setText("");
                jtfDni.setEnabled(false);
                jtfApellido1.setText("");
                jtfApellido1.setEnabled(false);
            }
        });
        jcbApellido1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfApellido1.setEnabled(true);
                jtfCodEmple.setText("");
                jtfCodEmple.setEnabled(false);
                jtfDni.setText("");
                jtfDni.setEnabled(false);
            }
        });
        //model del combobox
        Object[] tipoemple=new TipoEmpleadoDAO().getTiposEmpleado().toArray();
        jcbTipoEmple.setModel(new DefaultComboBoxModel(tipoemple));
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPfiltros = new javax.swing.JPanel();
        jcbCodEmple = new javax.swing.JCheckBox();
        jtfCodEmple = new javax.swing.JTextField();
        jcbTipoemple = new javax.swing.JCheckBox();
        jcbTipoEmple = new javax.swing.JComboBox();
        jcbDni = new javax.swing.JCheckBox();
        jtfDni = new javax.swing.JTextField();
        jcbApellido1 = new javax.swing.JCheckBox();
        jtfApellido1 = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        jbAltaEmple = new javax.swing.JButton();
        jbModificarEmple = new javax.swing.JButton();
        jbConsultarEmple = new javax.swing.JButton();
        jbEliminarEmple = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableEmpleados = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jPfiltros.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPfiltros.setLayout(new java.awt.GridLayout(4, 2));

        jcbCodEmple.setText("Codigo Empleado");
        jcbCodEmple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbCodEmpleActionPerformed(evt);
            }
        });
        jPfiltros.add(jcbCodEmple);
        jPfiltros.add(jtfCodEmple);

        jcbTipoemple.setText("Tipo Empleado");
        jcbTipoemple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTipoempleActionPerformed(evt);
            }
        });
        jPfiltros.add(jcbTipoemple);

        jcbTipoEmple.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Directivo", "Administrativo", "Medico", "Enfermero", "Auxiliar enfermeria" }));
        jPfiltros.add(jcbTipoEmple);

        jcbDni.setText("DNI");
        jPfiltros.add(jcbDni);
        jPfiltros.add(jtfDni);

        jcbApellido1.setText("Apellido1");
        jPfiltros.add(jcbApellido1);
        jPfiltros.add(jtfApellido1);

        jToolBar1.setRollover(true);

        jbAltaEmple.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_plus.png"))); // NOI18N
        jbAltaEmple.setFocusable(false);
        jbAltaEmple.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbAltaEmple.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbAltaEmple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAltaEmpleActionPerformed(evt);
            }
        });
        jToolBar1.add(jbAltaEmple);

        jbModificarEmple.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_edit1.png"))); // NOI18N
        jbModificarEmple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbModificarEmpleActionPerformed(evt);
            }
        });
        jToolBar1.add(jbModificarEmple);

        jbConsultarEmple.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_find.png"))); // NOI18N
        jbConsultarEmple.setFocusable(false);
        jbConsultarEmple.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbConsultarEmple.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbConsultarEmple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConsultarEmpleActionPerformed(evt);
            }
        });
        jToolBar1.add(jbConsultarEmple);

        jbEliminarEmple.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_cancel.png"))); // NOI18N
        jbEliminarEmple.setFocusable(false);
        jbEliminarEmple.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEliminarEmple.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbEliminarEmple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEliminarEmpleActionPerformed(evt);
            }
        });
        jToolBar1.add(jbEliminarEmple);

        jTableEmpleados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Cod.Empleado", "TipoEmpleado", "DNI", "Nombre", "Apellido 1", "Apellido 2", "Direccion", "Cod Postal", "Poblacion", "CCC"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableEmpleados);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(212, 212, 212)
                                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 1350, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1348, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jcbCodEmpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbCodEmpleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbCodEmpleActionPerformed

    private void jcbTipoempleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTipoempleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbTipoempleActionPerformed

    private void jbConsultarEmpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConsultarEmpleActionPerformed
        // TODO add your handling code here:
         if(jcbCodEmple.isSelected()||jcbDni.isSelected()||jcbTipoemple.isSelected()||jcbApellido1.isSelected()){
            alEmpleados=new ArrayList();
            EmpleadosDAO empleDao=new EmpleadosDAO();
            
            if(jcbCodEmple.isSelected()){
                if(jtfCodEmple.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Debes introducir datos en el campo de texto", "NO HAY DATOS",JOptionPane.INFORMATION_MESSAGE);
                }
                else{
                    try{
                        alEmpleados=(ArrayList)empleDao.buscaEmpleadosCodigo(Integer.parseInt(jtfCodEmple.getText()));
                    }
                    catch(NumberFormatException ex){
                        JOptionPane.showMessageDialog(this, "Debes introducir datos numericos en el campo de texto", "ERROR DE DATOS",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }  
            if(jcbDni.isSelected()){           
                    alEmpleados=(ArrayList)empleDao.buscaEmpleadosDni(jtfDni.getText());
            }
            if(jcbTipoemple.isSelected()){
                Tipoempleado te=(Tipoempleado)jcbTipoEmple.getSelectedItem();
                alEmpleados=(ArrayList)empleDao.buscaEmpleadosTipo(te);
            }
            if(jcbApellido1.isSelected()){
                    alEmpleados=(ArrayList)empleDao.buscaEmpleadosApellido(jtfApellido1.getText());
            }
            if(!alEmpleados.isEmpty()){
                //creo TableModel
                TableModelEmpleados tme = new TableModelEmpleados(alEmpleados);
                jTableEmpleados.setModel(tme);
                tme.fireTableDataChanged();//actualizo la tabla
            }
            else{
                JOptionPane.showMessageDialog(this, "No hay ningun empleado por esa consulta", "NO HAY DATOS",JOptionPane.WARNING_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Debes elegir algun filtro de busqueda", "ERROR DE CONSULTA",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jbConsultarEmpleActionPerformed

    private void jbEliminarEmpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEliminarEmpleActionPerformed
       try{
            EmpleadosDAO empleDao=new EmpleadosDAO();
            List<Empleados> lemple=empleDao.buscaEmpleadosCodigo(jTableEmpleados.getSelectedRow());
            int op=JOptionPane.showConfirmDialog(this, "Estas seguro de dar de baja a"+ lemple.get(0).getNombre(), "Confirmar Baja",JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
            if(op==JOptionPane.OK_OPTION){
                empleDao.eliminaEmpleado(lemple.get(0));
                JOptionPane.showMessageDialog(this, "Se ha dado de baja correctamente", "Baja correcta",JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(Exception ex){
           JOptionPane.showMessageDialog(this, "Debes seleccionar un empleado de la tabla", "No hay seleccion",JOptionPane.WARNING_MESSAGE);
       }
    }//GEN-LAST:event_jbEliminarEmpleActionPerformed

    private void jbAltaEmpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAltaEmpleActionPerformed
        new Jd_add_empleado(this);
    }//GEN-LAST:event_jbAltaEmpleActionPerformed

    private void jbModificarEmpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbModificarEmpleActionPerformed
        try{
            Jd_modificar_empleado ep = new Jd_modificar_empleado(this,alEmpleados.get(jTableEmpleados.getSelectedRow()));
            ep.setVisible(true);
       }catch(Exception ex){
           JOptionPane.showMessageDialog(this, "Debes seleccionar un empleado de la tabla", "No hay seleccion",JOptionPane.WARNING_MESSAGE);
       }
    }//GEN-LAST:event_jbModificarEmpleActionPerformed

 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPfiltros;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTable jTableEmpleados;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbAltaEmple;
    private javax.swing.JButton jbConsultarEmple;
    private javax.swing.JButton jbEliminarEmple;
    private javax.swing.JButton jbModificarEmple;
    private javax.swing.JCheckBox jcbApellido1;
    private javax.swing.JCheckBox jcbCodEmple;
    private javax.swing.JCheckBox jcbDni;
    private javax.swing.JComboBox jcbTipoEmple;
    private javax.swing.JCheckBox jcbTipoemple;
    private javax.swing.JTextField jtfApellido1;
    private javax.swing.JTextField jtfCodEmple;
    private javax.swing.JTextField jtfDni;
    // End of variables declaration//GEN-END:variables
}
